package org.example.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import org.example.config.R;
import org.example.domain.Users;
import org.example.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("users")
public class UsersController {
    @Autowired
    UsersService usersService;

    @RequestMapping("usersById")
    public R usersById(Integer id){
        List<Users> list = usersService.userById(id);
        return R.successed(list);
    }

    @RequestMapping("getUsername")
    public R getUsername(String username){
        List<Users> users = usersService.getUsername(username);
        return R.successed(users);
    }

    @RequestMapping("usersTree")
    public R usersTree(@RequestBody Users users){
       List<Users> usersList= usersService.list(users);
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();


        // 最大递归深度
        treeNodeConfig.setDeep(3);
//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<Users, String>build(usersList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("label",treeNode.getName());
                    tree.putExtra("id", treeNode.getId());
                });

        System.out.println(treeNodes);
        return R.successed(treeNodes);
    }

}
